package models

import (
	"good_srv/good"
	"gorm.io/gorm"
)

type Good struct {
	gorm.Model
	Title  string  `gorm:"type:varchar(150);not null;commit:'标题'"`
	Price  float64 `gorm:"type:decimal(10,2);not null;commit:'价格'"`
	Stock  int64   `gorm:"type:int;not null;commit:'库存'"`
	TypeId int64   `gorm:"type:int;not null;commit:'品类Id'"`
	Soles  int64   `gorm:"type:int;not null;commit:'销量'"`
	Image  string  `gorm:"type:varchar(150);not null;commit:'图片'"`
	Status int64   `gorm:"type:int;not null;commit:'商品状态: 1-上架,2-下架'"`
}

func (g *Good) GoodList() error {
	return DB.Where("id=?", g.ID).Limit(1).Find(&g).Error
}

func (g *Good) List(sort string) (goodlists []*good.GoodList) {
	query := DB.Model(&Good{})
	if sort == "soles" {
		query = query.Order("soles desc")
	} else if sort == "price" {
		query = query.Order("price desc")
	} else if sort == "type_id" {
		query = query.Order("type_id desc")
	}
	query.Find(&goodlists)
	return

}
